package com.app.newcsc.fragment.my.exchangepoints;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.app.newcsc.BR;
import com.app.newcsc.R;
import com.app.newcsc.app.AppApplication;
import com.app.newcsc.app.AppViewModelFactory;
import com.app.newcsc.databinding.FragmentExchangepointsBinding;
import com.app.newcsc.entity.ConfigEntity;
import com.app.newcsc.entity.ExchangeCoinEntity;
import com.app.newcsc.entity.ExchangeHomeEntity;
import com.app.newcsc.entity.Message;
import com.app.newcsc.fragment.my.exchangepoints.exchangepointsrecord.ExchangePointsRecordFragment;
import com.app.newcsc.utils.GlideUtils;
import com.app.newcsc.utils.NumberUtils;
import com.app.newcsc.utils.SpUtils;
import com.app.newcsc.utils.TextUtil;
import com.app.newcsc.utils.ToastUtil;
import com.app.newcsc.view.CustomPopWindow;
import com.app.newcsc.view.ExchangeHomeLeftAdapter;
import com.app.newcsc.view.ExchangeHomeRightAdapter;
import com.app.newcsc.view.InputPwdDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.bus.RxBus;
import me.goldze.mvvmhabit.bus.RxSubscriptions;

/**
 * @author: woking
 * @date: 2021/6/9
 * @description：
 */
public class ExchangePointsFragment extends BaseFragment<FragmentExchangepointsBinding, ExchangePointsViewModel> {

    private ExchangeCoinEntity.CoinExchange selectCoinLeft;
    private ExchangeCoinEntity.CoinExchange.ToCoin selectCoinRight;
    private double currHuiLv = 1;
    private double shouXuFei = 0;
    private boolean isSelectInput = true;

    public static boolean isRefreshExchange = false;
    private int lastPos = 0;

    private String type = "csc";
    public static ExchangePointsFragment newInstance(String type) {
        ExchangePointsFragment fragment = new ExchangePointsFragment();
        Bundle args = new Bundle();
        args.putString("type", type);
//        args.putString("time", time);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_exchangepoints;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public ExchangePointsViewModel initViewModel()
    {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用NetWorkViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(ExchangePointsViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        SpUtils.put(getActivity(),"isLogin","1");
        isRefreshExchange = false;
        if (getArguments() != null) {
            type = getArguments().getString("type");
        }
        Log.e("TAG","==========initData=====getArguments====type=="+type);

        listenerRefreshExchange();

        viewModel.pointWallet();
        viewModel.exchangeCoin();
//        viewModel.getZTXShopToken();
        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        binding.tvTitleRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startContainerActivity(ExchangePointsRecordFragment.class.getCanonicalName());
            }
        });
        binding.adLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initPOP(view,true);
            }
        });
        binding.jifenLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initPOP(view,false);
            }
        });
        binding.tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String amount = binding.inputNumEt.getText().toString();
                if(TextUtil.isEmpty(amount))
                {
                    ToastUtil.showToast("请输入兑换金额");
                    return;
                }
                if(selectCoinLeft != null)
                {
                    getMemberBaseInfo();
                }

            }
        });

        viewModel.mExchangeHomeEntity.observe(this, new Observer<ExchangeHomeEntity>() {
            @Override
            public void onChanged(ExchangeHomeEntity exchangeHomeEntity) {
//                selectCoinLeft = initPopData(exchangeHomeEntity, null,"AD").get(0);
//                Log.e("TAG",selectCoinLeft.coin_name+"=====AD=====0000=======积分=======");
//                selectCoinRight = initPopData(exchangeHomeEntity, null,"积分").get(0);
//                Log.e("TAG",selectCoinLeft.coin_name+"=====AD============积分======="+selectCoinRight.coin_name);
//                setViewText();
            }
        });
        viewModel.mExchangeFinish.observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                initGridView();
                clickItemGridView(AppApplication.ad_exchange_level);
            }
        });
        viewModel.mExchangeCoinEntity.observe(this, new Observer<ExchangeCoinEntity>() {
            @Override
            public void onChanged(ExchangeCoinEntity exchangeHomeEntity) {
                if(isRefreshExchange)//兑换成功的处理
                {
//                    Log.e("TAG",isRefreshExchange+"=======换成功的处理========selectCoinLeft.coin_name========"+selectCoinLeft.coin_name);
                    if(exchangeHomeEntity != null && exchangeHomeEntity.exchanged.size() > 0) {
                        for (int i = 0; i < exchangeHomeEntity.exchanged.size(); i++) {
                            if ("积分".equalsIgnoreCase(exchangeHomeEntity.exchanged.get(i).coin_name)) {
                                if (viewModel.mPointEntity.getValue() != null && viewModel.mPointEntity.getValue().point != null) {
                                    exchangeHomeEntity.exchanged.get(i).money = viewModel.mPointEntity.getValue().point;
                                }
                            }
                        }
                    }
                    for(int i = 0; i < exchangeHomeEntity.exchanged.size(); i++) {
                        if(selectCoinLeft.coin_name.equalsIgnoreCase(exchangeHomeEntity.exchanged.get(i).coin_name)) {
                            selectCoinLeft = exchangeHomeEntity.exchanged.get(i);
                            if(selectCoinLeft != null && selectCoinLeft.to_coin != null && selectCoinLeft.to_coin.size() > 0) {
                                selectCoinRight = selectCoinLeft.to_coin.get(lastPos);
                                if(type.equalsIgnoreCase("AD")) {
                                    for(ExchangeCoinEntity.CoinExchange.ToCoin toCoin : selectCoinLeft.to_coin) {
                                        if("积分".equalsIgnoreCase(toCoin.coin_name)) {
                                            selectCoinRight = toCoin;
                                        }
                                    }
                                }
                            }
                            break;
                        }
                    }
                    setViewText();
                }
                else
                {
                    if(exchangeHomeEntity != null && exchangeHomeEntity.exchanged.size() > 0)
                    {
                        for(int i = 0; i < exchangeHomeEntity.exchanged.size(); i++)
                        {
                            if("积分".equalsIgnoreCase(exchangeHomeEntity.exchanged.get(i).coin_name))
                            {
                                if(viewModel.mPointEntity.getValue() != null && viewModel.mPointEntity.getValue().point != null)
                                {
                                    exchangeHomeEntity.exchanged.get(i).money = viewModel.mPointEntity.getValue().point;
                                }
                            }
                        }
                        selectCoinLeft = exchangeHomeEntity.exchanged.get(0);
                        if(selectCoinLeft != null && selectCoinLeft.to_coin != null && selectCoinLeft.to_coin.size() > 0)
                        {
                            selectCoinRight = selectCoinLeft.to_coin.get(0);
                        }
                        for(int i = 0; i < exchangeHomeEntity.exchanged.size(); i++)
                        {
                            if(type.equalsIgnoreCase(exchangeHomeEntity.exchanged.get(i).coin_name))
                            {
                                selectCoinLeft = exchangeHomeEntity.exchanged.get(i);
                                if(selectCoinLeft != null && selectCoinLeft.to_coin != null && selectCoinLeft.to_coin.size() > 0)
                                {
                                    selectCoinRight = selectCoinLeft.to_coin.get(0);
                                    if(type.equalsIgnoreCase("AD"))
                                    {
                                        for(ExchangeCoinEntity.CoinExchange.ToCoin toCoin : selectCoinLeft.to_coin)
                                        {
                                            if("积分".equalsIgnoreCase(toCoin.coin_name))
                                            {
                                                selectCoinRight = toCoin;
                                            }
                                        }
                                    }
                                }
                                break;
                            }
                        }
                        setViewText();
                    }
                }
            }
        });

        binding.inputNumEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void afterTextChanged(Editable editable) {

                if(binding.inputNumEt.hasFocus())
                {
                    if(!TextUtil.isEmpty(binding.inputNumEt.getText().toString()))
                    {
                        double num = Double.parseDouble(binding.inputNumEt.getText().toString());
//                        Log.e("TAG", (selectCoinLeft != null) + "======yue==========inputNumEt====00000======num======" + (selectCoinRight != null));
                        if(selectCoinLeft != null && selectCoinRight != null)
                        {
//                            Log.e("TAG", selectCoinLeft.money+ "======money==========inputNumEt====00111000======num======" + (selectCoinRight != null));
                            if (!TextUtil.isEmpty(selectCoinLeft.money))
                            {
                                double yue = Double.parseDouble(selectCoinLeft.money);
                                if (num > yue)
                                {
                                    ToastUtil.showToast("余额不足！");
                                    binding.inputNumEt.setText(selectCoinLeft.money);
                                    return;
                                }
//                                Log.e("TAG", yue + "======yue==========inputNumEt====111111======num======" + num);
                            }
                        }
                    }
                    setLevelValue(-1);
                    setEditText(true);
                }
            }
        });
        binding.outputNumEt.setEnabled(false);
        binding.outputNumEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void afterTextChanged(Editable editable) {
//                if(binding.outputNumEt.hasFocus() && !TextUtil.isEmpty(editable.toString()))
//                {
//                    double num = Double.parseDouble(editable.toString());
//                    double duihuan = (num / ((1-shouXuFei)* currHuiLv));
//                    Log.e("TAG",num + "======num==========outputNumEt==========currHuiLv======" + currHuiLv + " shouXuFei:"+shouXuFei);
//                    binding.inputNumEt.setText(duihuan+"");
//                }
                if(binding.outputNumEt.hasFocus())
                {
                    setEditText(false);
                }
            }
        });

        initGridView();
    }

    private void setEditText(boolean isLeft)
    {
        if(isLeft)
        {
            if(!TextUtil.isEmpty(binding.inputNumEt.getText().toString()))
            {
                double num = Double.parseDouble(binding.inputNumEt.getText().toString());

                double duihuan = (num * currHuiLv) * (1-shouXuFei);
                double duihuan2 = NumberUtils.keepPrecision(duihuan,4);
//                DecimalFormat decimalFormat = new DecimalFormat("#,##0.0000");//格式化设置

//                Log.e("TAG",num + "======num==========inputNumEt==========currHuiLv======" + currHuiLv + " shouXuFei:"+shouXuFei);
//                    1.0======num==========inputNumEt==========currHuiLv======1.0 shouXuFei:50.0
//              binding.outputNumEt.setText(decimalFormat.format(duihuan2)+"");
                binding.outputNumEt.setText(duihuan2+"");
            }
            else
            {
                binding.outputNumEt.setText("");
            }
        }
        else
        {
            if(!TextUtil.isEmpty(binding.outputNumEt.getText().toString()))
            {
                double num = Double.parseDouble(binding.outputNumEt.getText().toString());
                double duihuan = (num / ((1-shouXuFei)* currHuiLv));
                Log.e("TAG",num + "======num==========outputNumEt==========currHuiLv======" + currHuiLv + " shouXuFei:"+shouXuFei);
                binding.inputNumEt.setText(NumberUtils.keepPrecision(duihuan,4)+""+"");
            }
            else
            {
                binding.inputNumEt.setText("");
            }
        }
    }


    private InputPwdDialog inputPasswordDialog;
    private void getMemberBaseInfo() {
        inputPasswordDialog = new InputPwdDialog(getContext(), new InputPwdDialog.InputPasswordListener() {
            @Override
            public void callbackPassword(String password) {
//                Log.e("TAG","====states====callbackPassword=====password=="+password);
                if(selectCoinLeft != null)
                {
                    if(binding.LeftCoinNameTv.getText().equals("积分")) {
                        if(selectCoinRight != null && !TextUtil.isEmpty(selectCoinRight.coin_name) && selectCoinRight.coin_name.equalsIgnoreCase("AD"))
                        {
                            //http://store.zhilingshenghuo.com/api/apiztxcsc/exchange_csc_wallet?pay_password=123456&money=1000&to_wallet_type=ad&wallet_type=point
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("money",binding.inputNumEt.getText().toString());
                            map.put("wallet_type","point");
                            map.put("to_wallet_type",selectCoinRight.coin_name.toLowerCase());
                            map.put("pay_password",password);
                            viewModel.pointExchange(map);
                        }
                        else
                        {
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("amount",binding.inputNumEt.getText().toString());
                            map.put("wallet_type",selectCoinLeft.coin_name);
                            map.put("to_wallet_type",selectCoinRight.coin_name);
                            map.put("payment_password",password);
                            viewModel.exchange(map);
                        }

                    }
                    else
                    {
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("amount",binding.inputNumEt.getText().toString());
                        map.put("coin_id",selectCoinLeft.id);
                        map.put("to_coin_id",selectCoinRight.id);
                        map.put("payment_password",password);
                        String leftStr = binding.LeftCoinNameTv.getText().toString();
                        String rightStr = binding.rightCoinNameTv.getText().toString();
                        if(leftStr.equalsIgnoreCase("AD") /*&& rightStr.equalsIgnoreCase("CSC")*/)
                        {
                            map.put("exchange_level",selectLevel);
                        }
//                    map.put("exchange_level",selectLevel);
                        viewModel.exchange(map);
                    }
                }
            }
        });
        inputPasswordDialog.setOnDismissListener(dialog -> {

        });
        inputPasswordDialog.show();
    }
    private CustomPopWindow mCustomPopWindow;
    private void initPOP(View view,boolean isLeft)
    {
        if(viewModel.mExchangeCoinEntity == null || viewModel.mExchangeCoinEntity.getValue() == null || viewModel.mExchangeCoinEntity.getValue().exchanged == null)
        {
            return;
        }
        View contentView = LayoutInflater.from(getContext()).inflate(R.layout.pop_duihuan_leixing,null);

        ListView listView = contentView.findViewById(R.id.listview_list);
//        ExchangeHomeAdapter adapter = new ExchangeHomeAdapter(getContext(),initPopData(viewModel.mExchangeHomeEntity.getValue(), selectCoin,null) );
//        listView.setAdapter(adapter);
        if(isLeft)
        {
            if(type.equalsIgnoreCase("CSC"))
            {
//                List<ExchangeCoinEntity.CoinExchange> exchangeds = new ArrayList<>();
//                for(ExchangeCoinEntity.CoinExchange coinExchange : viewModel.mExchangeCoinEntity.getValue().exchanged)
//                {
//                    if(!coinExchange.coin_name.equals("积分"))
//                    {
//                        exchangeds.add(coinExchange);
//                    }
//                }
//                ExchangeHomeLeftAdapter adapter = new ExchangeHomeLeftAdapter(getContext(),exchangeds);
//                listView.setAdapter(adapter);

                ExchangeHomeLeftAdapter adapter = new ExchangeHomeLeftAdapter(getContext(),viewModel.mExchangeCoinEntity.getValue().exchanged, type);
                listView.setAdapter(adapter);
            }
            else if(type.equalsIgnoreCase("AD"))
            {
                ExchangeHomeLeftAdapter adapter = new ExchangeHomeLeftAdapter(getContext(),viewModel.mExchangeCoinEntity.getValue().exchanged, type);
                listView.setAdapter(adapter);
            }
            else
            {
                ExchangeHomeLeftAdapter adapter = new ExchangeHomeLeftAdapter(getContext(),viewModel.mExchangeCoinEntity.getValue().exchanged);
                listView.setAdapter(adapter);
            }
        }
        else
        {
            if(selectCoinLeft == null || selectCoinLeft.to_coin == null || selectCoinLeft.to_coin.size() < 1)
            {
                return;
            }
            if(type.equalsIgnoreCase("AD") || type.equalsIgnoreCase("CSC"))
            {

                ExchangeHomeRightAdapter adapter = new ExchangeHomeRightAdapter(getContext(),selectCoinLeft.to_coin,viewModel.mExchangeCoinEntity.getValue().exchanged,type,viewModel);
                listView.setAdapter(adapter);
            }
            else
            {
                ExchangeHomeRightAdapter adapter = new ExchangeHomeRightAdapter(getContext(),selectCoinLeft.to_coin,viewModel.mExchangeCoinEntity.getValue().exchanged);
                listView.setAdapter(adapter);
            }
        }

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Log.e("TAG", "=======initPOP========"+ (selectCoin == null));
                if(isLeft)
                {
                    selectCoinLeft = (ExchangeCoinEntity.CoinExchange)adapterView.getAdapter().getItem(i);
                    selectCoinRight = selectCoinLeft.to_coin.get(0);
                }
                else
                {
                    lastPos = i;
                    selectCoinRight = (ExchangeCoinEntity.CoinExchange.ToCoin)adapterView.getAdapter().getItem(i);
                }
                setViewText();
                setEditText(true);
                mCustomPopWindow.dissmiss();
            }
        });

        //创建并显示popWindow
        mCustomPopWindow = new CustomPopWindow.PopupWindowBuilder(getContext())
                .setView(contentView)
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.3f) // 控制亮度
                .setFocusable(true)
                .setOutsideTouchable(true)
//                .size(600,400) //设置显示的大小，不设置就默认包裹内容
                .size(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)//显示大小
                .create()
                .showAtLocation(view, Gravity.BOTTOM,0,20);
//                .showAsDropDown(view,0,20);
    }

    private void setViewText()
    {
        if(selectCoinLeft != null && selectCoinRight != null)
        {
            binding.LeftCoinNameTv.setText(selectCoinLeft.coin_name);
            binding.rightCoinNameTv.setText(selectCoinRight.coin_name);
            GlideUtils.getInstances().loadNormalImg(getContext(),binding.LeftImg,selectCoinLeft.image);
            GlideUtils.getInstances().loadNormalImg(getContext(),binding.rightImg,selectCoinRight.image);
            binding.selectTypeTv.setText(selectCoinLeft.coin_name+"余额");


            double cscNum = Double.valueOf(selectCoinLeft.money) - Double.valueOf(selectCoinLeft.lock_s_one);
            binding.selectYueTv.setText(NumberUtils.keepPrecision(String.valueOf(cscNum), 4));
            binding.huilvTv.setText("1 "+selectCoinLeft.coin_name+" = "+ getHuiLv() + " " +selectCoinRight.coin_name);

            if (binding.rightCoinNameTv.getText().toString().equals("积分")){
                binding.shouxufeiTv.setText((Double.parseDouble(selectCoinLeft.jf_exchange_fee)*1) + "%");
                shouXuFei = Double.parseDouble(selectCoinLeft.jf_exchange_fee)/100;
            }else {
                binding.shouxufeiTv.setText((Double.parseDouble(selectCoinLeft.exchange_fee)*1) + "%");
                shouXuFei = Double.parseDouble(selectCoinLeft.exchange_fee)/100;
            }

            setSelectLevel();
        }
    }

    private double getHuiLv()
    {
        if(selectCoinLeft != null && selectCoinRight != null)
        {
//            for (Map.Entry<String, Object> entry : selectCoinLeft.exchange.entrySet())
//            {
//                if (entry.getKey().equals(selectCoinRight.id+""))
//                {
//                    Log.e("TAG","============getHuiLv============="+entry.getValue());
//                    currHuiLv = Double.parseDouble(entry.getValue()+"");
//                    return currHuiLv;
//                }
//            }
            currHuiLv = Double.parseDouble(selectCoinRight.rate+"");
            return currHuiLv;
        }
        currHuiLv = 0;
        return 0;
    }

    private ExchangeLevelAdapter mExchangeLevelAdapter;
    private int selectLevel = AppApplication.ad_exchange_level;
    private double feedtotal = 0;
    private void initGridView()
    {
        Map<String, ConfigEntity.LevelInfo> ad_level_exchange = AppApplication.ad_level_exchange;
        if(ad_level_exchange == null || ad_level_exchange.size() < 1)
        {
            return;
        }
        List<ConfigEntity.LevelInfo> datas = new ArrayList<>();
        for (Map.Entry<String, ConfigEntity.LevelInfo> entry : ad_level_exchange.entrySet())
        {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            ConfigEntity.LevelInfo levelInfo = new ConfigEntity.LevelInfo();
            levelInfo.condition_money = entry.getValue().condition_money;
            levelInfo.fee = entry.getValue().fee;

            String levelStr = entry.getKey();
            if(TextUtil.isEmpty(levelStr) || !levelStr.contains("level"))
            {
                levelInfo.info = "v0 50%";
                levelInfo.level = "0";
            }
            else
            {
                double feed = Double.parseDouble(entry.getValue().fee) * 100;
                String str = levelStr.replace("level","v");
                levelInfo.level = levelStr.replace("level","");
                levelInfo.info = str + " " +NumberUtils.keepPrecision(feed,1) + "%";
            }

            if(levelInfo.level.equals(AppApplication.ad_exchange_level))
            {
                levelInfo.isSelect = true;
            }

            Log.e("TAG",levelInfo.level + "=================initGridView============="+levelInfo.info);
            datas.add(levelInfo);
        }
        mExchangeLevelAdapter = new ExchangeLevelAdapter(getContext(),datas);
        binding.gridview.setAdapter(mExchangeLevelAdapter);
        binding.gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                clickItemGridView(i);
            }
        });
    }

    private void clickItemGridView(int index)
    {
        int level = Integer.parseInt(mExchangeLevelAdapter.getItem(index).level);
        if(AppApplication.ad_exchange_level <= level)
        {
            mExchangeLevelAdapter.setSelect(index);
            selectLevel = level;
            if(binding.selectLevelLl.getVisibility() == View.GONE)
            {
                binding.selectLevelLl.setVisibility(View.VISIBLE);
            }
            shouXuFei = Double.parseDouble(mExchangeLevelAdapter.getItem(index).fee);
            setLevelValue(level);
            setEditText(true);
        }
    }
    private void setLevelValue(int level)
    {
        if(level > -1)
        {
            Map<String, ConfigEntity.LevelInfo> ad_level_exchange = AppApplication.ad_level_exchange;
            if(ad_level_exchange == null || ad_level_exchange.size() < 1)
            {
                return;
            }

            feedtotal = 0;

            if(AppApplication.ad_exchange_level == level)
            {
                binding.selectLevelTv.setText("v"+AppApplication.ad_exchange_level);
                binding.selectLevelValueTv.setText(0+"AD");
            }
            else
            {
                binding.selectLevelTv.setText("v"+AppApplication.ad_exchange_level+"-v"+level);
                double feedtotalCurr = 0;
                for (Map.Entry<String, ConfigEntity.LevelInfo> entry : ad_level_exchange.entrySet())
                {
                    int leveltemp = 0;
                    String levelStr = entry.getKey();
                    if(TextUtil.isEmpty(levelStr) || !levelStr.contains("level"))
                    {
                        leveltemp = 0;
                    }
                    else
                    {
                        String strlevel = levelStr.replace("level","");
                        leveltemp = Integer.parseInt(strlevel);
                    }
                    if(leveltemp == AppApplication.ad_exchange_level)
                    {
                        feedtotalCurr = Double.parseDouble(entry.getValue().condition_money);
                    }
//                    if(leveltemp > AppApplication.ad_exchange_level && leveltemp <= level)
                    if(leveltemp > AppApplication.ad_exchange_level && leveltemp == level)
                    {
                        feedtotal = feedtotal + Double.parseDouble(entry.getValue().condition_money);
                    }
                }
                if(feedtotal > 0)
                {
                    feedtotal = feedtotal-feedtotalCurr;
                    if(feedtotal < 0)
                    {
                        feedtotal = 0;
                    }
                    binding.selectLevelValueTv.setText(NumberUtils.keepPrecision(feedtotal,4)+"AD");
                }
            }
        }

        double inputNum = 0;
        if(!TextUtil.isEmpty(binding.inputNumEt.getText().toString()))
        {
            inputNum = Double.parseDouble(binding.inputNumEt.getText().toString());
        }
        double total = inputNum + feedtotal;
        binding.totalLevelTv.setText(NumberUtils.keepPrecision(total,4)+"AD");
    }

    private void setSelectLevel()
    {
        String leftStr = binding.LeftCoinNameTv.getText().toString();
        String rightStr = binding.rightCoinNameTv.getText().toString();
        if(leftStr.equalsIgnoreCase("AD") && rightStr.equalsIgnoreCase("CSC"))
        {
            if(binding.gridview.getVisibility()==View.GONE)
            {
                clickItemGridView(AppApplication.ad_exchange_level);
                binding.shouxufeiTv.setVisibility(View.GONE);
                binding.gridview.setVisibility(View.VISIBLE);
            }
        }
        else
        {
            if(binding.shouxufeiTv.getVisibility()==View.GONE)
            {
                binding.shouxufeiTv.setVisibility(View.VISIBLE);
                binding.gridview.setVisibility(View.GONE);
                binding.selectLevelLl.setVisibility(View.GONE);
            }
        }
    }

    private Disposable mSubscription;
    private void listenerRefreshExchange()
    {
        mSubscription = RxBus.getDefault().toObservable(Message.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(new Consumer<Message>() {
                    @Override
                    public void accept(final Message message) throws Exception {
                        Log.e("TAG",type + "===============message=============="+message.getName());
                        if(message.getName().equals("RefreshExchange"))
                        {
                            viewModel.pointWallet();
                        }
                    }
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mSubscription != null)
        {
            RxSubscriptions.remove(mSubscription);
        }
    }
}